// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPawn.h"

AMyPawn::AMyPawn()
{
 	PrimaryActorTick.bCanEverTick = true;

	_box = CreateDefaultSubobject<UBoxComponent>(TEXT("PawnBox"));
	SetRootComponent(_box);
}

void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
}

void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AMyPawn::SetupPlayerInputComponent(UInputComponent* inputComponent)
{
	Super::SetupPlayerInputComponent(inputComponent);

	inputComponent->BindAction("Home", IE_Pressed, this, &AMyPawn::OnHome);
	inputComponent->BindAxis("MoveL", this, &AMyPawn::OnMoveLeft);
	inputComponent->BindAxis("MoveR", this, &AMyPawn::OnMoveRight);
	inputComponent->BindAxis("MoveD", this, &AMyPawn::OnMoveDown);
	inputComponent->BindAxis("MoveU", this, &AMyPawn::OnMoveUp);
	inputComponent->BindVectorAxis(EKeys::Mouse2D, this, &AMyPawn::OnMouseMove);
}

void AMyPawn::OnHome()
{
	SetActorLocation(FVector(160.0, 0.0, 60.0));
}

void AMyPawn::OnMoveLeft(float v)
{
	if (v == 0.0f) return;
	FVector vec = -GetActorRightVector() * v;
	SetActorLocation(GetActorLocation() + vec);
}

void AMyPawn::OnMoveRight(float v)
{
	if (v == 0.0f) return;
	FVector vec = GetActorRightVector() * v;
	SetActorLocation(GetActorLocation() + vec);
}

void AMyPawn::OnMoveDown(float v)
{
	if (v == 0.0f) return;
	AddActorWorldOffset(FVector(0.0, 0.0, -v));
}

void AMyPawn::OnMoveUp(float v)
{
	if (v == 0.0f) return;
	AddActorWorldOffset(FVector(0.0, 0.0, v));
}

void AMyPawn::OnMouseMove(FVector v)
{
	AddControllerYawInput(v.X);
	AddControllerPitchInput(-v.Y);
}